home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Utilities / DVIM72-Mac 1.9.6 / source / Select_folder.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-14  |  5.1 KB  |  196 lines  |  [TEXT/R*ch]

  1. #include <Script.h>
  2. #include "mac-specific.h"
  3. Boolean TrapAvailable( short trapnum );
  4. void Param_string( StringPtr out, StringPtr format,
  5.     StringPtr p0, StringPtr p1, StringPtr p2 );
  6.  
  7. #define SFGETFOLDER_DLOG_ID        4045
  8. #define GESTALT            0xA1AD
  9.  
  10. #define DEBUG 0
  11.  
  12. #if DEBUG
  13. #define CKPT(x)        DebugStr("\p" x);
  14. #define ASSERT(x,y)        if (!(x)) {DebugStr("\p" y);}
  15. #else
  16. #define CKPT(x)
  17. #define ASSERT(x,y)
  18. #endif
  19.  
  20. void Select_folder( char *folder_path );
  21.  
  22. static pascal short dialog_hook( short item, DialogPtr dialog_ptr );
  23. static void Get_folder_pathname ( StringPtr PathNamePtr);
  24.  
  25. Boolean g_folder_selected;
  26. long    g_folder_dirID;
  27. short    g_folder_vRefNum;
  28.  
  29. ControlHandle    g_folder_button;
  30. SFReply        g_reply;    /* global so the dialog hook can see it */
  31.  
  32. /* ------------------------- Select_folder ---------------------------- */
  33. void Select_folder( char *folder_path )
  34. {
  35.     Point        where;
  36.     CursHandle    watch_cursor;
  37.     
  38.     CKPT( "Select_folder start" );
  39.     g_folder_selected = false;
  40.     g_folder_dirID = NIL;
  41.     g_folder_button = NIL;
  42.     g_folder_vRefNum = -SFSaveDisk;
  43.     where = Get_SF_place( SFGETFOLDER_DLOG_ID );
  44.     SFPGetFile(where, "\pfile:", nil, -1, nil,
  45.         (ProcPtr)dialog_hook, &g_reply, SFGETFOLDER_DLOG_ID, nil );
  46.     if (g_folder_selected)
  47.     {
  48.         CKPT( "Select_folder selected" );
  49.         watch_cursor = GetCursor( watchCursor );
  50.         SetCursor( *watch_cursor );
  51.         Get_folder_pathname( (StringPtr) folder_path );
  52.         InitCursor();
  53.         if (folder_path[0] == 0)
  54.             Show_error( "Sorry, full pathname too long." );
  55.         else
  56.             PtoCstr( (StringPtr) folder_path );
  57.     }
  58.     else
  59.         folder_path[0] = 0;
  60.     CKPT( "Select_folder end" );
  61. }
  62.  
  63.  
  64.  
  65.  
  66. /* --------------------- Get_folder_pathname ----------------- */
  67. /* 
  68.     Use the low-memory globals set by Standard File,
  69.     SFSaveDisk = negative of last volume reference number,
  70.     CurDirStore = last directory ID
  71.     to find the full path name of the selected folder.
  72. */
  73. static void Get_folder_pathname ( StringPtr PathNamePtr)
  74. {
  75. Str255    ParVolName;
  76. int err, i;
  77. char *bufPtr, *name_ptr;
  78. char buf[500];
  79. Boolean        HaveAUX;
  80. CInfoPBRec    block;
  81.  
  82.     HaveAUX = (HWCfgFlags & (1 << 9)); /* a low-mem global */
  83.  
  84. /* We'll fill in buf from right to left. */
  85.     buf[499] = '\0';
  86.     bufPtr = buf + 499;
  87.     
  88.     block.dirInfo.ioNamePtr = (StringPtr) ParVolName;
  89.     block.dirInfo.ioDrParID = g_folder_dirID;
  90.     block.dirInfo.ioVRefNum = g_folder_vRefNum;
  91.     block.dirInfo.ioFDirIndex = -1;
  92.  
  93.     do {
  94.         block.dirInfo.ioDrDirID = block.dirInfo.ioDrParID;
  95.  
  96.         err = PBGetCatInfo(&block, FALSE);
  97.         if (HaveAUX) {
  98.             if (ParVolName[1] != '/')
  99.                 /* If this isn't root (i.e. '/'), append a slash ('/') */
  100.                 *(--bufPtr) = '/';
  101.         } else 
  102.             /* Append a Macintosh style colon (':') */
  103.             *(--bufPtr) = ':';
  104.         i = (unsigned char) ParVolName[0]; /* length of name */
  105.         name_ptr = (char *)ParVolName + i + 1;
  106.         for (; i; i--)
  107.             *(--bufPtr) = *(--name_ptr);
  108.     } while (block.dirInfo.ioDrDirID != 2);
  109.  
  110.     if ( (buf + 499) - bufPtr > 255 )
  111.     {
  112.         PathNamePtr[0] = 0;    /* string too long; return null string */
  113.     }
  114.     else /* copy all, including final colon */
  115.     {
  116.         PathNamePtr[0] = (buf + 499) - bufPtr;
  117.         BlockMove( bufPtr, &PathNamePtr[1], (buf + 499) - bufPtr );
  118.     }
  119. } /* Get_folder_pathname */
  120.  
  121. #define FOLDER_BUTTON    11
  122.  
  123. /* -------------------------- dialog_hook ---------------------------- */
  124. static pascal short dialog_hook( short item, DialogPtr dialog_ptr )
  125. {
  126.     short    retval;
  127.     short    itype;
  128.     Rect    box;
  129.     long    this_dirID;
  130.     short    this_vRefNum;
  131.     Str255    button_title, folder_name;
  132.     CInfoPBRec    block;
  133.     OSErr    err;
  134.     static Boolean trunc_folder;
  135.     static short    title_room;
  136.     static Str255    folder_name_format;
  137.     long    response;
  138.     
  139.     retval = item;
  140.     if ( (g_reply.fName[0] == 0) && (g_reply.fType != NIL) )
  141.         this_dirID = g_reply.fType;
  142.     else
  143.         this_dirID = CurDirStore;
  144.     this_vRefNum = -SFSaveDisk;
  145.     switch (item)
  146.     {
  147.         case sfHookFirstCall:
  148.             GetDItem( dialog_ptr, FOLDER_BUTTON, &itype,
  149.                 (Handle *) &g_folder_button, &box );
  150.             GetCTitle( g_folder_button, folder_name_format );
  151.             title_room = (box.right - box.left)
  152.                 - StringWidth(folder_name_format);
  153.             trunc_folder = TrapAvailable(GESTALT) &&
  154.                 (Gestalt(gestaltScriptMgrVersion,&response) == noErr) &&
  155.                 (response >= 0x0700L);
  156.             /*
  157.                 Note: I purposely let this case fall through to the
  158.                 null event case, so the buttons title will be set sooner.
  159.             */
  160.         case sfHookNullEvent:
  161.             if ( (this_dirID != g_folder_dirID) /* update button title */
  162.                 || (this_vRefNum != g_folder_vRefNum) )
  163.             {
  164.                 g_folder_dirID = this_dirID;
  165.                 g_folder_vRefNum = this_vRefNum;
  166.                 block.dirInfo.ioCompletion = NIL;
  167.                 block.dirInfo.ioNamePtr = folder_name;
  168.                 block.dirInfo.ioFDirIndex = -1;
  169.                 block.dirInfo.ioVRefNum = g_folder_vRefNum;
  170.                 block.dirInfo.ioDrDirID = g_folder_dirID;
  171.                 err = PBGetCatInfo( &block, false );
  172.                 if (err == noErr)
  173.                 {
  174.                     if (trunc_folder)
  175.                             (void) TruncString( title_room, folder_name,
  176.                             smTruncMiddle );
  177.                     Param_string( button_title, folder_name_format,
  178.                         folder_name, NIL, NIL );
  179.                     SetCTitle( g_folder_button, button_title );
  180.                 }
  181.             }
  182.             break;
  183.         case FOLDER_BUTTON:
  184.             g_folder_selected = TRUE;
  185.             retval = getCancel; /* return fake Cancel to dismiss the dialog */
  186.             break;
  187.         case getOpen:
  188.             SysBeep(1);
  189.             retval = sfHookNullEvent;
  190.             break;
  191.     }
  192.  
  193.     return retval;
  194. }
  195.  
  196.